
(function($) {
    'use strict';
    $(function() {
        var number ="0";
        getvalidTabls();
        $(".peoplenumber").keyup(function(){
            number = $(".peoplenumber").val();
            if (number<=0){
                $.alert("就餐人数至少为1人");
            }
            if (number>0 && number<=4){
                number=4;
            }
            if (number>4 && number<=8){
                number=8;
            }
            if (number>8){
                number=12;
            }
            if (number!=""){
                $.ajax({
                    type: "get",
                    url: "/api/table/valid",    //向后端请求数据的url
                    data: {
                        peoplenum:number
                    },
                    success: function (data) {
                        $("#tableselect").empty();
                        console.log(data)
                        if (data.length>0){
                            for(var i=0;i<data.length;i++){
                                var dom='  <option value="'+data[i].tableNo+'">'+data[i].tableNo+"号餐桌"+'</option>';
                                $("#tableselect").append(dom);
                            }
                        }else {
                            $.alert("暂无可用餐桌");

                        }


                    }
                });
            }


        });

        $("#submitBtn").click(function () {
            var username = $(".username").val();
            var phonenumber = $(".phonenumber").val();
            var peoplenumber = $(".peoplenumber").val();
            var arrivaltime = $(".arrvaltime").val();
            var tableno = $('#tableselect option:selected').val();
            function p(s) {
                return s < 10 ? '0' + s: s;
            }

            var myDate = new Date();
           //获取当前年
            var year=myDate.getFullYear();
           //获取当前月
            var month=myDate.getMonth()+1;
           //获取当前日
            var date=myDate.getDate();
            var h=myDate.getHours();       //获取当前小时数(0-23)
            var m=myDate.getMinutes();     //获取当前分钟数(0-59)
            var s=myDate.getSeconds();
            var now=year+'-'+p(month)+"-"+p(date)+" "+p(h)+':'+p(m)+":"+p(s);
            $.ajax({
                type: "post",
                url: "/api/bookingrecord",    //向后端请求数据的url
                data: {
                    customername:username,
                    phonenumber:phonenumber,
                    peoplenum:peoplenumber,
                    tableno:tableno,
                    arrivaltime:arrivaltime,
                    bookingtime:now,
                    isarrival:0,
                    isover:0,
                    recordcancle:0
                },
                success: function (data) {
                    if (data){
                        $.alert("顾客 "+data.customer.name+" 预定成功");
                        $(".username").val("");
                        $(".phonenumber").val("");
                        $(".peoplenumber").val("");
                        $(".arrvaltime").val("");
                        $("#tableselect").empty();
                        getvalidTabls();
                    }else {
                        $.alert("系统错误，预定失败！");
                    }

                }
            });

        });


        function getvalidTabls() {
            $.ajax({
                type: "get",
                url: "/api/table/validlist",    //向后端请求数据的url
                data: {},
                success: function (data) {
                    if (data.length>0){
                        $(".validtables").empty();
                        for(var i=0;i<data.length;i++){
                            var dom=' <tr>\n' +
                                '<td>'+data[i].tableNo+"号餐桌"+'</td>\n' +
                                '<td>'+data[i].size +"人"+'</td>\n' +
                                '<td>可用</td>\n' +
                                '</tr>';
                            $(".validtables").append(dom);
                        }
                    }else {
                        $.alert("暂无可用餐桌");
                    }

                }
            });
        }






    });

})(jQuery);